/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CommandBar.java
 *
 * Created on Apr 4, 2011, 8:16:43 PM
 */

package net.worlds.component;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import net.worlds.Chat;
import net.worlds.Main;
import net.worlds.ui.CustomBorder;
import net.worlds.ui.CustomButton;

/**
 *
 * @author Jacob Morgan
 */
public class CommandBar extends javax.swing.JPanel {
    private String lastCommand = null;
    /** Creates new form CommandBar */
    public CommandBar() {
        initComponents();
        setBackground(new Color(0, 0, 0, 0));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        executeButton = new CustomButton();
        commandTextField = new javax.swing.JTextField();

        setForeground(new Color(0, 0, 0, 0));

        executeButton.setText("Run");
        executeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                executeButtonActionPerformed(evt);
            }
        });

        commandTextField.setBackground(new java.awt.Color(123, 128, 154));
        commandTextField.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        commandTextField.setForeground(new java.awt.Color(28, 36, 78));
        commandTextField.setText("");
        commandTextField.setBorder(new CustomBorder(new Color(28, 36, 78)));
        commandTextField.addKeyListener(new java.awt.event.KeyAdapter()
        {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                if(evt.getKeyCode() == KeyEvent.VK_ENTER)
                    executeButtonActionPerformed(null);
                else if(evt.getKeyCode() == KeyEvent.VK_UP)
                {
                    if(lastCommand != null)
                        commandTextField.setText(lastCommand);
                }
                else if(evt.getKeyCode() == KeyEvent.VK_DOWN)
                {
                    commandTextField.setText("");
                }
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(commandTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 671, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(executeButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(executeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(commandTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>

    private void executeButtonActionPerformed(java.awt.event.ActionEvent evt) {
        final String s = commandTextField.getText();
        lastCommand = s;
        if(!Main.isBuildClient())
        {
            System.out.println(">"+s);
            commandTextField.setText("");
            if(s.equals("cls"))
            {
                Chat.clear();
            }
            else
                Main.getScriptContext().createScriptThread(null, "Script_Command", s).start();
        }
        else
            Main.runInMainThread(new Runnable()
            {
                public void run() {
                    Main.getClient().sendScript(s);
                }
            });
    }


    // Variables declaration - do not modify
    private javax.swing.JTextField commandTextField;
    private javax.swing.JButton executeButton;
    // End of variables declaration

}
